package com.moyq5.permit.center.web.controller;

import com.moyq5.permit.center.web.form.MerchAdminForm4Edit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.moyq5.mvc.framework.general.Body;
import com.moyq5.mvc.framework.general.PageData;
import com.moyq5.mvc.framework.jackson.result.ObjectResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult.Status;
import com.moyq5.permit.center.bean.embed.MerchAdminId;
import com.moyq5.permit.center.bean.entity.Merch;
import com.moyq5.permit.center.bean.entity.MerchAdmin;
import com.moyq5.permit.center.bean.search.MerchAdmin4Search;
import com.moyq5.permit.center.feign.client.MerchAdminFeign;
import com.moyq5.permit.center.web.jackson.filter.MerchAdminFilter;

/**
 * 商户管理员
 * @author Moyq5
 * @date 2019年7月25日
 */
@RestController
@RequestMapping("/merchAdmin")
public class MerchAdminController extends BaseController {
	
	private static Logger log = LoggerFactory.getLogger(MerchAdminController.class);
	
	@Autowired
	private MerchAdminFeign merchAdminFeign;
	
	/**
	 * @author Moyq5
	 * @date 2019年7月30日
	 * @param merchAdmin4s
	 * @param pd
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping(value="/page", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String page(MerchAdmin4Search merchAdmin4s, PageData pd) throws JsonProcessingException {
		log.debug("查询代理商管理员信息>>>page(MerchAdmin4Search, PageData)");
		if (null != getLoginMerchId()) {
			if (null == merchAdmin4s.getMerch()) {
				merchAdmin4s.setMerch(new Merch());
			}
			merchAdmin4s.getMerch().setMerchId(getLoginMerchId());;
		}
		return toJson(merchAdminFeign.page(new Body<MerchAdmin4Search, PageData>(merchAdmin4s, pd)), MerchAdminFilter.get());
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param merchAdmin4s
	 * @param pd
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping(value="/page/admin", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String pageWithAdmin(MerchAdmin4Search merchAdmin4s, PageData pd) throws JsonProcessingException {
		log.debug("查询商户管理员信息>>>pageWithAdmin(MerchAdmin4Search, PageData)");
		if (null != getLoginMerchId()) {
			if (null == merchAdmin4s.getMerch()) {
				merchAdmin4s.setMerch(new Merch());
			}
			merchAdmin4s.getMerch().setMerchId(getLoginMerchId());;
		}
		return toJson(merchAdminFeign.pageWithAdmin(new Body<MerchAdmin4Search, PageData>(merchAdmin4s, pd)), MerchAdminFilter.getWithAdmin());
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param merchAdmin4s
	 * @param pd
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping(value="/page/merchAndAdmin", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String pageWithMerchAndAdmin(MerchAdmin4Search merchAdmin4s, PageData pd) throws JsonProcessingException {
		log.debug("查询商户管理员信息>>>pageWithMerchAndAdmin(MerchAdmin4Search, PageData)");
		if (null != getLoginMerchId()) {
			if (null == merchAdmin4s.getMerch()) {
				merchAdmin4s.setMerch(new Merch());
			}
			merchAdmin4s.getMerch().setMerchId(getLoginMerchId());;
		}
		return toJson(merchAdminFeign.pageWithMerchAndAdmin(new Body<MerchAdmin4Search, PageData>(merchAdmin4s, pd)), MerchAdminFilter.getWithMerchAndAdmin());
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param merchAdmin
	 * @return
	 */
	@PutMapping("/add")
	@ResponseBody
	public ObjectResult<MerchAdminId> add(@RequestBody MerchAdmin merchAdmin) {
		log.debug("添加商户管理员信息>>>add(MerchAdmin)");
		if (null != getLoginMerchId()) {
			merchAdmin.getId().setMerchId(getLoginMerchId());
		}
		ObjectResult<MerchAdmin> objRst = merchAdminFeign.one(merchAdmin.getId());
		if (objRst.getStatus() == Status.OK && null != objRst.getData()) {
			ObjectResult<MerchAdminId> idRst = new ObjectResult<MerchAdminId>();
			idRst.setOk();
			idRst.setData(merchAdmin.getId());
			return idRst;
		}
		return merchAdminFeign.create(merchAdmin);
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param merchAdmin
	 * @return
	 */
	@PatchMapping("/edit")
	@ResponseBody
	public StatusResult edit(@RequestBody @Validated MerchAdminForm4Edit merchAdmin) {
		log.debug("更新商户管理员信息>>>edit(MerchAdmin)");
		if (null != getLoginMerchId()) {
			merchAdmin.getId().setMerchId(getLoginMerchId());
		}
		return merchAdminFeign.update(merchAdmin);
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param merchAdminId
	 * @return
	 */
	@DeleteMapping("/del")
	@ResponseBody
	public StatusResult del(@RequestBody MerchAdminId merchAdminId) {
		log.debug("删除商户管理员信息>>>del(MerchAdminId)");
		if (null != getLoginMerchId()) {
			merchAdminId.setMerchId(getLoginMerchId());
		}
		return merchAdminFeign.remove(merchAdminId);
	}
	
}
